<?php

namespace WeForge\Runtime;

class Response
{
    /**
     * @var string
     */
    protected $body;

    /**
     * @var int
     */
    protected $status;

    /**
     * @var array
     */
    protected $headers;

    /**
     * Create a new Response instance.
     *
     * @param string|array $body
     * @param int $status
     * @param array $headers
     */
    public function __construct($body, $status = 200, $headers = [])
    {
        if (is_array($body)) {
            $body = json_encode($body);

            $headers['Content-Type'] = 'application/json';
        }

        $this->body = $body;
        $this->status = $status;
        $this->headers = $headers;
    }

    /**
     * @param  \Swoole\Http\Response $response
     *
     * @return void
     */
    public function send($response)
    {
        $response->status($this->status);
        $response->header = $this->headers;
        $response->end($this->body);
    }
}
